package com.yzg.simple.netty.unpacking.unpacking_solve.codec;

import com.yzg.simple.netty.unpacking.unpacking_solve.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author misterWei
 * @create 2020年01月07号:01点31分
 * @mailbox mynameisweiyan@gmail.com
 */
public class MessageEncode extends MessageToByteEncoder<MessageProtocol> {
    //自定义 编码器
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        //先写入该int 类型字节 4 个字节;当然存储的内容也是在int返回内的
        out.writeInt(msg.getLen());
        //再写入该字节码数据
        out.writeBytes(msg.getContent());


    }
}
